var Admin = require("../../../class/admin/admin.c");
var _Admin = new Admin();
module.exports = {
    prefix: "/admin/",
    post: {
        "create": {
            des: {
                name: "createadmin",

            },
            method: function* (next) {
                var self = this;
                try {
                    var data = yield parse(self.request);
                    print("创建管理员", data);
                    var a = yield new Admin(data).save();
                    self.body = a;
                } catch (e) {
                    printError(e);
                    self.body = returnErrorBody(e.toString());
                }
            }
        },
        "login": {
            des: {
                name: "adminlogin",
                content: "管理员登陆",
                params: [
                    {
                        key: "username",
                        type: "string"
                    },
                    {
                        key: "password",
                        type: "string"
                    }]
            },
            method: co.wrap(function* (ctx) {
                var self = this;
                try {
                    var data = yield parse(self.request);
                    var _a = yield _Admin.login(data);
                    // console.log(_a);
                    self.session.admin = _a || "";
                    self.body = _a;
                } catch (e) {
                    printError(e);
                    self.body = returnErrorBody(e);
                };
            })
        },
        "loginer": {
            des: {
                name: "loginWithSession",
                content: "获取已登录管理员",
                params: []
            },
            method: function* () {
                var self = this;
                print("获取已登陆管理员")
                if (!self.session.admin) {
                    self.body = printError("没有找到登陆用户");
                    return;
                };
                self.body = self.session.admin;
            }
        },
        "loginout": {
            des: {
                name: "adminLoginOut",
                content: "管理员退出登陆"
            },
            method: function* (next) {
                var self = this;
                print("管理员退出登陆");
                self.session.admin = "";
                self.body = {};
                yield next;
            }
        }
    },
    put: {
        "update": {
            des: {
                name: "updateAdminInfo",
                content: "更新管理员信息",
                params: [
                    {
                        key: "admin",

                    }
                ]
            },
            method: function* (next) {
                var self = this;
                print("更新管理员信息");
                try {
                    var data = yield parse(self.request) || {};
                    if (!$.testMobile(data.mobile)) {
                        self.body = printError("请填写正确手机号");
                        return;
                    };
                    if (data.email && !$.testEmail(data.email)) {
                        self.body = printError("邮箱格式不正确");
                        return;
                    };
                    if (!data.verifycode) {
                        self.body = printError("请填写验证码");
                        return;
                    };
                    print(data);
                    try {
                        print("验证短信验证码");
                        var _obj = yield bmob.sms.verifySmsCode(data.mobile, data.verifycode);
                        print("短信验证结果：", _obj);
                        if (_obj.msg == "ok") {
                            var _a = yield _Admin.update(data);
                            self.session.admin = _a;
                            self.body = _a;
                        };
                    } catch (e) {
                        switch (e.code) {
                            case 207:
                                self.body = printError("验证码错误或已过期");
                                break;
                            default:
                                self.body = printError(e.message);
                                break;
                        };
                        return;
                    };
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "changepassword": {
            des: {
                name: "adminChangePassword",
                content: "修改管理员密码",
                params: [
                    {
                        key: "oldpassword",
                        type: "string"
                    },
                    {
                        key: "newpassword",
                        type: "string"
                    },
                    {
                        key: "verifycode",
                        type: "Number|String"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("修改管理员密码");
                try {
                    var data = yield parse(self.request);
                    if (!data) {
                        body = printError("请填写完整信息");
                    } else if (!data.oldpassword) {
                        body = printError("请填写旧密码");
                    } else if (!data.newpassword) {
                        body = printError("请填写新密码");
                    } else if (data.newpassword.length < 6) {
                        body = printError("密码长度不能小于6位");
                    } else {
                        // 验证短信验证码
                        try {
                            var status = yield bmob.sms.verifySmsCode(self.session.admin.mobile, data.verifycode);
                            print("验证短信验证码：", status);
                            if (status.msg == "ok") {
                                data._id = self.session.admin._id;
                                body = yield _Admin.changepassword(data);
                            } else {
                                body = printError("验证码错误");
                            };
                        } catch (e) {
                            switch (e.code) {
                                case 207:
                                    body = printError("验证码错误");
                                    break;
                                default:
                                    body = printError(e.message);
                                    break;
                            };
                        };
                    };
                } catch (e) {
                    body = printError(e)
                };
                self.body = body;
            }
        }
    }
}